package com.caipiao.crawler.zhongjiangRule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.caipiao.crawler.football.FootballResultPipeline;
import com.caipiao.crawler.mapper.LotteryFootballMapper;
import com.caipiao.crawler.mapper.LotteryTicketMapper;
import com.caipiao.crawler.model.LotteryFootball;
import com.caipiao.crawler.model.LotteryTicket;
import com.caipiao.util.DBTools;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FootballRule {

    @Data
    @AllArgsConstructor
    public static class Input {
        private double bet;
        private String matchId;
    }

    public static void footballPrizeRule() {
        SqlSession session = DBTools.getSession();
        LotteryFootballMapper mapper = session.getMapper(LotteryFootballMapper.class);
        LotteryTicketMapper ticketMapper = session.getMapper(LotteryTicketMapper.class);
        //TODO 触发更新判断是否中奖的逻辑
        Map ticketMap = new HashMap();
        ticketMap.put("type", "5");
        ticketMap.put("prize_status", "0");
        ticketMap.put("status", "1");
        List<LotteryTicket> ticketList = ticketMapper.selectFootballTicket(ticketMap);
        for (LotteryTicket ticket : ticketList) {
            String number = ticket.getNumber();
            int id = ticket.getId();
            int betsmode = ticket.getBetsmode();
            JSONArray array = JSON.parseArray(number);
            Boolean flag = true;  //该订单所有赛事是否已经结束
            List<Input> inputs = new ArrayList<Input>();
            float prize_amount = 0;
            String cluster = ticket.getCluster(); //串关方式
            for (int i = 0; i < array.size(); i++) {
                JSONObject ticketJson = (JSONObject) array.get(i);
                String mid = ticketJson.getString("mid");
                List<LotteryFootball> modelList = mapper.selectModelByMid(mid);

                if (modelList != null) {
                    LotteryFootball model = modelList.get(0);
                    int first_scroe = model.getFirst_scroe();
                    int second_score = model.getSecond_score();
                    int first_half_scroe = model.getFirst_half_score();
                    int second_half_score = model.getSecond_half_score();
                    if (model.getStatus() == 3) {
                        if (betsmode == 1) {
                            //单关方式
                            int lotid = ticket.getLotid();
//                                151竞彩足球，胜平负
//                                152竞彩足球，比分
//                                153竞彩足球总进球数
//                                154竞彩足球半全场胜平负
//                                156竞彩足球让球胜平负
                            if (lotid == 151) {
                                String guess = ticketJson.getString("guess");
                                String[] guessArray = guess.split(",");
                                String pl = ticketJson.getString("odds"); //赔率
                                String[] plArray = pl.split(",");
                                for (int j = 0; j < guessArray.length; j++) {
                                    if (first_scroe > second_score && "3".equals(guessArray[j])) {
                                        prize_amount += 2 * Float.parseFloat(plArray[j]);
                                    } else if (first_scroe == second_score && "1".equals(guessArray[j])) {
                                        prize_amount += 2 * Float.parseFloat(plArray[j]);
                                    } else if (first_scroe < second_score && "0".equals(guessArray[j])) {
                                        prize_amount += 2 * Float.parseFloat(plArray[j]);
                                    }
                                }
                            } else if (lotid == 152) {
                                String guess = ticketJson.getString("guess");
                                String[] guessArray = guess.split(",");
                                String pl = ticketJson.getString("odds"); //赔率
                                String[] plArray = pl.split(",");
                                for (int j = 0; j < guessArray.length; j++) {
                                    String prizeResult = "";
                                    if (first_scroe + second_score <= 7 &&
                                            !((first_scroe + "" + second_score).equals("43") ||
                                                    (first_scroe + "" + second_score).equals("34"))) {
                                        prizeResult = first_scroe + "" + second_score;
                                        if (prizeResult.equals(guessArray[j])) {
                                            prize_amount += 2 * Float.parseFloat(plArray[j]);
                                        }
                                    } else if (first_scroe > second_score) {
                                        prizeResult = "90";
                                        if (prizeResult.equals(guessArray[j])) {
                                            prize_amount += 2 * Float.parseFloat(plArray[j]);
                                        }
                                    } else if (first_scroe == second_score) {
                                        prizeResult = "99";
                                        if (prizeResult.equals(guessArray[j])) {
                                            prize_amount += 2 * Float.parseFloat(plArray[j]);
                                        }
                                    } else if (first_scroe < second_score) {
                                        prizeResult = "09";
                                        if (prizeResult.equals(guessArray[j])) {
                                            prize_amount += 2 * Float.parseFloat(plArray[j]);
                                        }
                                    }
                                }
                            } else if (lotid == 153) {
                                String guess = ticketJson.getString("guess");
                                String[] guessArray = guess.split(",");
                                String pl = ticketJson.getString("odds"); //赔率
                                String[] plArray = pl.split(",");
                                for (int j = 0; j < guessArray.length; j++) {
                                    int score = first_scroe + second_score > 7 ? 7 : first_scroe + second_score;
                                    if (String.valueOf(score).equals(guessArray[j])) {
                                        prize_amount += 2 * Float.parseFloat(plArray[j]);
                                    }
                                }
                            } else if (lotid == 154) {
                                String guess = ticketJson.getString("guess");
                                String[] guessArray = guess.split(",");
                                String pl = ticketJson.getString("odds"); //赔率
                                String[] plArray = pl.split(",");
                                String score = "";
                                if (first_half_scroe > second_half_score) {
                                    score = score + "3";
                                } else if (first_half_scroe == second_half_score) {
                                    score = score + "1";
                                } else if (first_half_scroe < second_half_score) {
                                    score = score + "0";
                                }
                                if (first_scroe > second_score) {
                                    score = score + "3";
                                } else if (first_scroe == second_score) {
                                    score = score + "1";
                                } else if (first_scroe < second_score) {
                                    score = score + "0";
                                }
                                for (int j = 0; j < guessArray.length; j++) {
                                    if (score.equals(guessArray[j])) {
                                        prize_amount += 2 * Float.parseFloat(plArray[j]);
                                    }
                                }
                            } else if (lotid == 156) {
                                JSONObject details = (JSONObject) JSONObject.parse(model.getDetails());
                                JSONObject detail1 = details.getJSONObject("detail1");
                                String rangqiu = detail1.getString("让球胜平负");
                                int rang = Integer.parseInt(rangqiu.substring(1, rangqiu.length() - 1));
                                String guess = ticketJson.getString("guess");
                                String[] guessArray = guess.split(",");
                                String pl = ticketJson.getString("odds"); //赔率
                                String[] plArray = pl.split(",");
                                for (int j = 0; j < guessArray.length; j++) {
                                    if (first_scroe + rang > second_score && "3".equals(guessArray[j])) {
                                        prize_amount += 2 * Float.parseFloat(plArray[j]);
                                    } else if (first_scroe + rang == second_score && "1".equals(guessArray[j])) {
                                        prize_amount += 2 * Float.parseFloat(plArray[j]);
                                    } else if (first_scroe + rang < second_score && "0".equals(guessArray[j])) {
                                        prize_amount += 2 * Float.parseFloat(plArray[j]);
                                    }
                                }


                            }
                        } else if (betsmode == 2) {
                            //混关模式
                            String gametype = ticketJson.getString("gametype");
//                                gametype取值：
//                                1胜平负
//                                2比分
//                                3总进球数
//                                4半全场胜平负
//                                6让球胜平负
                            if ("1".equals(gametype)) {
                                String guess = ticketJson.getString("guess");
                                String[] guessArray = guess.split(",");
                                String pl = ticketJson.getString("odds"); //赔率
                                String[] plArray = pl.split(",");
                                for (int j = 0; j < guessArray.length; j++) {
                                    if (first_scroe > second_score && "3".equals(guessArray[j])) {
                                        inputs.add(new Input(Double.parseDouble(plArray[j]), mid));
                                    } else if (first_scroe == second_score && "1".equals(guessArray[j])) {
                                        inputs.add(new Input(Double.parseDouble(plArray[j]), mid));
                                    } else if (first_scroe < second_score && "0".equals(guessArray[j])) {
                                        inputs.add(new Input(Double.parseDouble(plArray[j]), mid));
                                    }
                                }

                            } else if ("2".equals(gametype)) {

                                String guess = ticketJson.getString("guess");
                                String[] guessArray = guess.split(",");
                                String pl = ticketJson.getString("odds"); //赔率
                                String[] plArray = pl.split(",");
                                for (int j = 0; j < guessArray.length; j++) {
                                    String prizeResult = "";
                                    if (first_scroe + second_score <= 7 &&
                                            !((first_scroe + "" + second_score).equals("43") ||
                                                    (first_scroe + "" + second_score).equals("34"))) {
                                        prizeResult = first_scroe + "" + second_score;
                                        if (prizeResult.equals(guessArray[j])) {
                                            inputs.add(new Input(Double.parseDouble(plArray[j]), mid));
                                        }
                                    } else if (first_scroe > second_score) {
                                        prizeResult = "90";
                                        if (prizeResult.equals(guessArray[j])) {
                                            inputs.add(new Input(Double.parseDouble(plArray[j]), mid));
                                        }
                                    } else if (first_scroe == second_score) {
                                        prizeResult = "99";
                                        if (prizeResult.equals(guessArray[j])) {
                                            inputs.add(new Input(Double.parseDouble(plArray[j]), mid));
                                        }
                                    } else if (first_scroe < second_score) {
                                        prizeResult = "09";
                                        if (prizeResult.equals(guessArray[j])) {
                                            inputs.add(new Input(Double.parseDouble(plArray[j]), mid));
                                        }
                                    }
                                }
                            } else if ("3".equals(gametype)) {

                                String guess = ticketJson.getString("guess");
                                String[] guessArray = guess.split(",");
                                String pl = ticketJson.getString("odds"); //赔率
                                String[] plArray = pl.split(",");
                                for (int j = 0; j < guessArray.length; j++) {
                                    int score = first_scroe + second_score > 7 ? 7 : first_scroe + second_score;
                                    if (String.valueOf(score).equals(guessArray[j])) {
                                        inputs.add(new Input(Double.parseDouble(plArray[j]), mid));
                                    }
                                }
                            } else if ("4".equals(gametype)) {

                                String guess = ticketJson.getString("guess");
                                String[] guessArray = guess.split(",");
                                String pl = ticketJson.getString("odds"); //赔率
                                String[] plArray = pl.split(",");
                                String score = "";
                                if (first_half_scroe > second_half_score) {
                                    score = score + "3";
                                } else if (first_half_scroe == second_half_score) {
                                    score = score + "1";
                                } else if (first_half_scroe < second_half_score) {
                                    score = score + "0";
                                }
                                if (first_scroe > second_score) {
                                    score = score + "3";
                                } else if (first_scroe == second_score) {
                                    score = score + "1";
                                } else if (first_scroe < second_score) {
                                    score = score + "0";
                                }
                                for (int j = 0; j < guessArray.length; j++) {
                                    if (score.equals(guessArray[j])) {
                                        inputs.add(new Input(Double.parseDouble(plArray[j]), mid));
                                    }
                                }
                            } else if ("6".equals(gametype)) {

                                JSONObject details = (JSONObject) JSONObject.parse(model.getDetails());
                                JSONObject detail1 = details.getJSONObject("detail1");
                                String rangqiu = detail1.getString("让球胜平负");
                                int rang = Integer.parseInt(rangqiu.substring(1, rangqiu.length() - 1));
                                String guess = ticketJson.getString("guess");
                                String[] guessArray = guess.split(",");
                                String pl = ticketJson.getString("odds"); //赔率
                                String[] plArray = pl.split(",");
                                for (int j = 0; j < guessArray.length; j++) {
                                    if (first_scroe + rang > second_score && "3".equals(guessArray[j])) {
                                        inputs.add(new Input(Double.parseDouble(plArray[j]), mid));
                                    } else if (first_scroe + rang == second_score && "1".equals(guessArray[j])) {
                                        inputs.add(new Input(Double.parseDouble(plArray[j]), mid));
                                    } else if (first_scroe + rang < second_score && "0".equals(guessArray[j])) {
                                        inputs.add(new Input(Double.parseDouble(plArray[j]), mid));
                                    }
                                }
                            }

                        }
                    } else {
                        flag = false;
                        prize_amount = 0;
                        inputs = null;
                        break;
                    }
                }
            }
            //所有赛事已全部结束，计算中奖结果
            if (flag) {
                int multiple = ticket.getMultiple();
                if (betsmode == 1) {
                    Map updateMap = new HashMap();
                    if (prize_amount == 0) {
                        updateMap.put("id", id);
                        updateMap.put("prize_amount", prize_amount*multiple);
                        updateMap.put("is_prize", "0");
                        updateMap.put("prize_status", "1");
                        updateMap.put("updated_time", (int) (System.currentTimeMillis() / 1000L));
                        ticketMapper.updateTicketByIdForFootball(updateMap);
                    } else {
                        updateMap.put("id", id);
                        updateMap.put("prize_amount", prize_amount*multiple);
                        updateMap.put("is_prize", "1");
                        updateMap.put("prize_status", "1");
                        updateMap.put("updated_time", (int) (System.currentTimeMillis() / 1000L));
                        ticketMapper.updateTicketByIdForFootball(updateMap);
                    }
                } else if (betsmode == 2) {
                    prize_amount = (float) FootballRule.calMax(inputs, Integer.parseInt(cluster));
                    Map updateMap = new HashMap();
                    if (prize_amount == 0) {
                        updateMap.put("id", id);
                        updateMap.put("prize_amount", prize_amount*multiple*2*inputs.size());
                        updateMap.put("is_prize", "0");
                        updateMap.put("prize_status", "1");
                        updateMap.put("updated_time", (int) (System.currentTimeMillis() / 1000L));
                        ticketMapper.updateTicketByIdForFootball(updateMap);
                    } else {
                        updateMap.put("id", id);
                        updateMap.put("prize_amount", prize_amount*multiple*2*inputs.size());
                        updateMap.put("is_prize", "1");
                        updateMap.put("prize_status", "1");
                        updateMap.put("updated_time", (int) (System.currentTimeMillis() / 1000L));
                        ticketMapper.updateTicketByIdForFootball(updateMap);
                    }
                }
            }

        }
        session.commit();
        session.close();
    }

    public static double calMax(List<Input> inputs, int num) {
        if(CollectionUtils.isEmpty(inputs)) {
            return 0;
        }
        double sumBet = 0;
        for (int i = 0; i < inputs.size(); i++) {
            Input input = inputs.get(i);
            double nowBet = input.getBet();
            if(num > 1) {
                nowBet *= calMax(inputs.subList(i + 1, inputs.size()), num - 1);
            }
            sumBet += nowBet;
        }
        return sumBet;
    }

    public static void main(String []args) {
        footballPrizeRule();
    }
}
